# CCode to COW function

# Created Jan 31 2012
# Brian Greenhill

# This converts between COW 3-digit country codes and the 3-letter abbreviations.  The function works in both directions and will determine whether you have supplied the numeric or character abbreviations.

# Arguments:
# countries: vector containing COW 3-digit country codes OR 3-letter COW abbreviations.

# Values:
# output: vector containing 3-letter COW abbreviations OR 3-digit country codes.


# This converts between COW ccodes and the 3-letter COW abbreviations.  Both ccode2cow and cow2ccode are identical (bidirectional) functions.

ccode2cow<-ccode2cow<-function(countries){
	
	if (any(is.na(countries))) stop("Missing values in your country ID vector.")
	
	countrylist<-unique(as.character(countries))
	
	# determine whether the user supplied ccodes or cow abbreviations
	if (any(is.character(countries)|is.factor(countries))) ccodes.provided<-FALSE else ccodes.provided<-TRUE
	
	if (ccodes.provided) mismatchcode<-"x" else mismatchcode<-0
	
	
	keydata<-c(
	"USA",	2,
	"CAN",	20,
	"BHM",	31,
	"CUB",	40,
	"HAI",	41,
	"DOM",	42,
	"JAM",	51,
	"TRI",	52,
	"BAR",	53,
	"DMA",	54,
	"GRN",	55,
	"SLU",	56,
	"SVG",	57,
	"AAB",	58,
	"SKN",	60,
	"MEX",	70,
	"BLZ",	80,
	"GUA",	90,
	"HON",	91,
	"SAL",	92,
	"NIC",	93,
	"COS",	94,
	"PAN",	95,
	"COL",	100,
	"VEN",	101,
	"GUY",	110,
	"SUR",	115,
	"ECU",	130,
	"PER",	135,
	"BRA",	140,
	"BOL",	145,
	"PAR",	150,
	"CHL",	155,
	"ARG",	160,
	"URU",	165,
	"UKG",	200,
	"IRE",	205,
	"NTH",	210,
	"BEL",	211,
	"LUX",	212,
	"FRN",	220,
	"MNC",	221,
	"LIE",	223,
	"SWZ",	225,
	"SPN",	230,
	"AND",	232,
	"POR",	235,
	"HAN",	240,
	"BAV",	245,
	"GMY",	255,
	"GFR",	260,
	"GDR",	265,
	"BAD",	267,
	"SAX",	269,
	"WRT",	271,
	"HSE",	273,
	"HSG",	275,
	"MEC",	280,
	"POL",	290,
	"AUH",	300,
	"AUS",	305,
	"HUN",	310,
	"CZE",	315,
	"CZR",	316,
	"SLO",	317,
	"ITA",	325,
	"PAP",	327,
	"SIC",	329,
	"SNM",	331,
	"MOD",	332,
	"PMA",	335,
	"TUS",	337,
	"MLT",	338,
	"ALB",	339,
	"MNG",	341,
	"MAC",	343,
	"CRO",	344,
	"YUG",	345,
	"BOS",	346,
	"KOS",	347,
	"SLV",	349,
	"GRC",	350,
	"CYP",	352,
	"BUL",	355,
	"MLD",	359,
	"ROM",	360,
	"RUS",	365,
	"EST",	366,
	"LAT",	367,
	"LIT",	368,
	"UKR",	369,
	"BLR",	370,
	"ARM",	371,
	"GRG",	372,
	"AZE",	373,
	"FIN",	375,
	"SWD",	380,
	"NOR",	385,
	"DEN",	390,
	"ICE",	395,
	"CAP",	402,
	"STP",	403,
	"GNB",	404,
	"EQG",	411,
	"GAM",	420,
	"MLI",	432,
	"SEN",	433,
	"BEN",	434,
	"MAA",	435,
	"NIR",	436,
	"CDI",	437,
	"GUI",	438,
	"BFO",	439,
	"LBR",	450,
	"SIE",	451,
	"GHA",	452,
	"TOG",	461,
	"CAO",	471,
	"NIG",	475,
	"GAB",	481,
	"CEN",	482,
	"CHA",	483,
	"CON",	484,
	"DRC",	490,
	"UGA",	500,
	"KEN",	501,
	"TAZ",	510,
	"ZAN",	511,
	"BUI",	516,
	"RWA",	517,
	"SOM",	520,
	"DJI",	522,
	"ETH",	530,
	"ERI",	531,
	"ANG",	540,
	"MZM",	541,
	"ZAM",	551,
	"ZIM",	552,
	"MAW",	553,
	"SAF",	560,
	"NAM",	565,
	"LES",	570,
	"BOT",	571,
	"SWA",	572,
	"MAG",	580,
	"COM",	581,
	"MAS",	590,
	"SEY",	591,
	"MOR",	600,
	"ALG",	615,
	"TUN",	616,
	"LIB",	620,
	"SUD",	625,
	"IRN",	630,
	"TUR",	640,
	"IRQ",	645,
	"EGY",	651,
	"SYR",	652,
	"LEB",	660,
	"JOR",	663,
	"ISR",	666,
	"SAU",	670,
	"YAR",	678,
	"YEM",	679,
	"YPR",	680,
	"KUW",	690,
	"BAH",	692,
	"QAT",	694,
	"UAE",	696,
	"OMA",	698,
	"AFG",	700,
	"TKM",	701,
	"TAJ",	702,
	"KYR",	703,
	"UZB",	704,
	"KZK",	705,
	"CHN",	710,
	"MON",	712,
	"TAW",	713,
	"KOR",	730,
	"PRK",	731,
	"ROK",	732,
	"JPN",	740,
	"IND",	750,
	"BHU",	760,
	"PAK",	770,
	"BNG",	771,
	"MYA",	775,
	"SRI",	780,
	"MAD",	781,
	"NEP",	790,
	"THI",	800,
	"CAM",	811,
	"LAO",	812,
	"DRV",	816,
	"RVN",	817,
	"MAL",	820,
	"SIN",	830,
	"BRU",	835,
	"PHI",	840,
	"INS",	850,
	"ETM",	860,
	"AUL",	900,
	"PNG",	910,
	"NEW",	920,
	"VAN",	935,
	"SOL",	940,
	"KIR",	946,
	"TUV",	947,
	"FIJ",	950,
	"TON",	955,
	"NAU",	970,
	"MSI",	983,
	"PAL",	986,
	"FSM",	987,
	"WSM",	990
	)

	# put it in matrix form (this will force everything into character format, which is OK)
	keydata<-matrix(keydata, ncol=2, byrow=T)
	
	# create a blank outputkey vector:
	outputkey<-rep(mismatchcode, length(countrylist))
	
	# define which of the schemes to select:
	if (ccodes.provided) selector<-2 else selector<-1
	
	# helper function:
	reverser<-function(x) as.numeric(!as.logical(x-1))+1
	
	# First do a search using exact matching:
	for (i in 1:length(countrylist)){
		index<-keydata[,selector]==countrylist[i]
		if (sum(index)==1) outputkey[i]<-keydata[,reverser(selector)][index]
		} # close i loop
	
	# Report the mismatches:
	if (any(outputkey==mismatchcode)){
		cat("The following countries could not be identified and have been assigned a value of ", mismatchcode, ":","\n",sep="")
		print(countrylist[outputkey==mismatchcode])
		} # close mismatch procedure
	
	# Now use the outputkey to populate the output vector:
	output<-rep(NA, length(countries))
	for (i in 1:length(output)){
		output[i]<-outputkey[countrylist==countries[i]]
		} # close i loop
	
	if (!ccodes.provided) output<-as.numeric(output)
	
	output
	}
